/**
 * 公司汇总情况-导购
 * - 从 src\pages\Gskftj\index.js 复制而来，基本一致
 * - 2025-11-06 19:13:22 tzj
 * */

import BaseComponent from "../../components/BaseComponent";
import {mrhzApi} from "../../services/basic";
import {addPrecise} from "../../utils/math";

// 要显示的form表单
const formItemList = [
  "companies",
  {
    name: 'personZw',
    label: '导购',
    keyword: 'dg',
    zw: '3'
  },
  {
    name: 'personZw',
    label: '联系人',
    keyword: 'lxr',
    zw: ''
  },
  "check",
  {
    name: "dateRange",
    label: "留资日期",
    start: "qsrq",
    end: "jsrq"
  },
];
// 显示的按钮
const buttons = ["query", "export",];
// 表格列信息
const columns = [
  {
    title: '公司/人员',//xm
    tip: '公司名、销售/客服/负责人',
    dataIndex: 'gsmc',
    // width: "20%",
    sumName: "合计"
  },
  {
    title: '总到店',
    tip: "到店已成交+到店未成交+异公司到店已成交+异公司到店未成交",
    dataIndex: 'zdd',
    render: (_, record) => Number(record.ddwcj) + Number(record.ddycj) + Number(record.ygscj) + Number(record.ygsdd),
    formula: "=SUM({{ddwcj}},{{ddycj}},{{ygscj}},{{ygsdd}})",
    sorter: true
  },
  {
    title: '到店未成交',
    dataIndex: 'ddwcj',
    sorter: true
  },
  {
    title: '到店已成交',
    dataIndex: 'ddycj',
    sorter: true
  },
  {
    title: '到店已成交总金额',
    tip: '包括本店+异店成交',
    dataIndex: 'cjje',
    sorter: true
  },
  {
    title: '异公司已成交',
    dataIndex: 'ygscj',
    sorter: true
  },
  {
    title: '异公司到店未成交',
    dataIndex: 'ygsdd',
    sorter: true
  },
  {
    title: '成交率（%）',
    dataIndex: 'wxcjl',
    tip: '成交率 = （本店+异店）成交 ÷ 总到店 × 100%',
    render: (_, record) => {
      const zcj = Number(record.ddycj) + Number(record.ygscj)
      const zdd = Number(record.ddwcj) + Number(record.ygsdd) + zcj
      return (zcj * 100 / zdd).toFixed(2)
    },
    numFmt: "0.00%",
    formula: "=({{ddycj}}+{{ygscj}})/{{zdd}}",
    sorter: true,
  },
  {
    title: '客单价',
    tip: '客单价 = 到店已成交总金额/（异公司成交数+本店到店成交）',
    render: (_, record) => {
      const denominator = (Number(record.ddycj) + Number(record.ygscj))
      return denominator ? (Number(record.cjje) / denominator).toFixed(2) : 0
    },
    numFmt: "0.00",
    formula: "=({{cjje}}/({{ddycj}}+{{ygscj}})",
    dataIndex: 'wxkdj',
    sorter: true
  },
]

class pageGscjtj extends BaseComponent {
  constructor(props) {
    super(props);
    this.state = {
      ...this.state,
      formItemList,
      buttons,
      // columns,
      filename: "鹏成车邦分公司客服留资情况报表",
      tableTitle: true,
      queryAll: true,
      sumRow: true,
      expandable: {
        expandRowByClick: true,
        rowExpandable: (record) => !!record.children, // 可选：控制哪些行可以展开
      }
    }
    this.columns = columns;
    this.queryApi = mrhzApi.selectPagegsdg;
    this.hasLzModal = true;
  }

  dataProcess(arr) {
    console.log(arr)
    const map = new Map();

    arr.forEach(item => {
      const obj = {
        ...item,
        gsmc: item.xm,
        jgmc: item.jgmc,
      }
      if (map.has(item.gsmc)) {
        const existing = map.get(item.gsmc);
        existing.ddwcj = addPrecise((existing.ddwcj || 0), (obj.ddwcj || 0))
        existing.ddycj = addPrecise((existing.ddycj || 0), (obj.ddycj || 0))
        existing.ygscj = addPrecise((existing.ygscj || 0), (obj.ygscj || 0))
        existing.ygsdd = addPrecise((existing.ygsdd || 0), (obj.ygsdd || 0))
        existing.cjje = addPrecise((existing.cjje || 0), (obj.cjje || 0))
        existing.yxlz = addPrecise((existing.yxlz || 0), (obj.yxlz || 0))
        existing.children.push(obj);
      } else {
        map.set(item.gsmc, {
          ...item,
          gsmc: item.gsmc,
          children: [obj],
        });
      }
    });

    // 转换Map为数组并添加层级key
    const list = Array.from(map.values()).map((parent, parentIndex) => {
      const parentKey = (parentIndex + 1).toString();

      const childrenWithKeys = parent.children.map((child, childIndex) => ({
        ...child,
        key: `${parentKey}-${childIndex + 1}`
      }));

      return {
        ...parent,
        key: parentKey,
        children:childrenWithKeys
      };
    });
    console.log(list)
    return list
  }

  onRowDoubleClick = (record) => {
    if (record.children) return;
    const params = {
      ...this.form.dataForm,
      yybm: record.yybm,
      sfzh: record.zhbh,
      lxr: record.gh,
      // lxr: 'KF013'
    }
    params.zzrq = params.jsrq;
    delete params.jsrq;
    this.lzModalRef.open(params, {});
  }
}

export default pageGscjtj;
